<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <h2>用户注册</h2>
        </div>
      </template>
      
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef">
        <el-form-item prop="email">
          <el-input 
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            prefix-icon="Message">
          </el-input>
        </el-form-item>

        <el-form-item prop="username">
          <el-input 
            v-model="registerForm.username"
            placeholder="请输入用户名"
            prefix-icon="User">
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password>
          </el-input>
        </el-form-item>

        <el-form-item prop="realName">
          <el-input 
            v-model="registerForm.realName"
            placeholder="请输入真实姓名"
            prefix-icon="User">
          </el-input>
        </el-form-item>

        <el-form-item prop="checkCode">
          <div class="check-code-container">
            <el-input 
              v-model="registerForm.checkCode"
              placeholder="请输入验证码"
              style="width: 200px">
            </el-input>
            <img 
              :src="checkCodeImage" 
              @click="refreshCheckCode"
              class="check-code-image"
              alt="验证码">
          </div>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
        </el-form-item>
        
        <div class="register-footer">
          <span>已有账号？</span>
          <router-link to="/login">返回登录</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const router = useRouter()
const registerFormRef = ref(null)
const checkCodeImage = ref('')

const registerForm = reactive({
  email: '',
  username: '',
  password: '',
  realName: '',
  checkCode: '',
  checkCodeKey: ''
})

const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  checkCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

// 获取验证码
const getCheckCode = async () => {
  try {
    const res = await axios.get('/account/checkCode')
    if (res.data.status === 'success') {
      checkCodeImage.value = res.data.data.checkCode
      registerForm.checkCodeKey = res.data.data.checkCodeKey
    }
  } catch (error) {
    ElMessage.error('获取验证码失败')
  }
}

// 刷新验证码
const refreshCheckCode = () => {
  getCheckCode()
}

// 注册处理
const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const res = await axios.post('/account/register', registerForm)
        if (res.data.status === 'success') {
          ElMessage.success('注册成功')
          router.push('/login')
        }
      } catch (error) {
        ElMessage.error(error.response?.data?.info || '注册失败')
        refreshCheckCode()
      }
    }
  })
}

onMounted(() => {
  getCheckCode()
})
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.register-card {
  width: 400px;
}

.card-header {
  text-align: center;
}

.check-code-container {
  display: flex;
  gap: 10px;
}

.check-code-image {
  height: 32px;
  cursor: pointer;
}

.register-footer {
  text-align: center;
  margin-top: 20px;
}
</style> 